package vn.edu.saigontech.ncms.service.impl;

import java.util.Collection;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import vn.edu.saigontech.ncms.model.system.Location;
import vn.edu.saigontech.ncms.service.LocationService;

public class LocationServiceImpl extends HibernateGenericService<Location, Long> implements LocationService {

	public Location add(String name, String comment) {
		Location building = new Location();
		building.setName(name);
		building.setComment(comment);
		building.setActive(true);
		return super.persist(building);
	}

	public Location edit(Long locationId, String name, boolean active, String comment) {
		Location building = this.findById(locationId);
		building.setName(name);
		building.setActive(active);
		building.setComment(comment);
		return super.persist(building);
	}

	@SuppressWarnings("unchecked")
	public Collection<Location> findAllAreActive() {
		Criteria criteria = this.createCriteria();
		criteria.add(Restrictions.eq("active", true));
		criteria.addOrder(Order.asc("name"));
		return criteria.list();
	}
}